local dbg = require 'jass.debug'
local jass = require 'jass.common'

local class = { }
setmetatable(class,class)
ac.selector.class['mover'] = class

-- 范围选择
class['range'] = function(self,info)
    local p = self._point
    local r = self._range
    local enum = ac.mover.enum_range(p,r)
    local loop = #enum
    while true do
        local h = enum[loop]
        if h and h ~= 0 then
            loop = loop - 1 
            local u = ac.mover.j_mover(h)
            if u and self:do_filter(u) then 
                table.insert(info,u)
            end
        else
            break
        end 
    end
end

-- 扇形选择
class['sector'] = function(self,info)
    local p = self._point 
    local x,y = p:get()
    local r = self._range
    local a = self._angle 
    local s = self.section / 2 
    local enum = ac.mover.enum_range(p,r)
    local loop = #enum
    while true do 
        local h = enum[loop]
        if h and h ~= 0 then
            loop = loop - 1 
            local u = ac.mover.j_mover(h)
            if u and self:do_filter(u) and ac.math_angle(a,p / u:get_point()) <= s then 
                table.insert(info,u)
            end
        else
            break
        end 
    end 
end

-- 矩形选择
class['rect'] = function(self,info)
    local rct = self._rect
    local w,h = (- rct)
    local p = rct:get_point()
    local r = math.sqrt((w/2)^2 + (h/2)^2)
    local enum = ac.mover.enum_range(p,r)
    local loop = #enum
    while true do 
        local h = enum[loop]
        if h and h ~= 0 then
            loop = loop - 1 
            local u = ac.mover.j_mover(h)
            if u and self:do_filter(u) and rct:has(u) then 
                table.insert(info,u)
            end
        else
            break
        end 
    end 
end
 
-- 直线选择
class['line'] = function(self,info)
    local s = self._point 
    local t = s - {self._angle, self._len}
    local x1, y1 = s:get()
    local x2, y2 = t:get()
    local a, b = y1 - y2, x2 - x1
    local c = - a * x1 - b * y1
    local l = (a * a + b * b) ^ 0.5
    local w = self._width / 2
    local r = self._len / 2
    local x, y = (x1 + x2) / 2, (y1 + y2) / 2
    local p = ac.point(x, y) 
    local enum = ac.mover.enum_range(p, r + 256)
    local loop = #enum
    while true do 
        local h = enum[loop]
        if h and h ~= 0 then
            loop = loop - 1 
            local u = ac.mover.j_mover(h)
            local x, y = u:get_point():get()
            local d = math.abs(a * x + b * y + c) / l
            if d <= w + u:get_collision() and self:do_filter(u) then
                table.insert(info,u)
            end
        else
            break
        end 
    end 
end


local mt = { }
class.__index = mt 
extends(ac.selector)(mt)

-- 是敌人
function mt:enemy(target)
    return self:filter(function(dest)
        return dest:is_enemy(target)
    end)
end 

-- 是友军
function mt:ally(target)
    return self:filter(function(dest)
        return dest:is_ally(target)
    end)
end 

-- 选取
function mt:select()
    local name = self._type 
    if name and class[name] then 
        local info = { }
        class[name](self,info)
        return info
    end 
end 

-- 过滤
function mt:do_filter(u)
    if not u.mover.type == 'effect' then 
        return false 
    end
    return ac.selector.do_filter(self,u)
end 

return class 